//************************************************************
//  Copyright (c) 深圳市赛元微电子有限公司
//	文件名称	: sc92f_timer2.c
//	作者		:
//	模块功能	: TIMER2固件库函数C文件
//	局部函数列表:
//  最后更正日期: 2022/01/22
// 	版本		: V1.10003
//  说明        :
//*************************************************************

#include "sc92f_timer2.h"

#if !defined (SC92L853x) && !defined(SC92L753x)

/**************************************************
*函数名称:void TIM2_DeInit(void)
*函数功能:TIMER2相关寄存器复位至缺省值
*入口参数:void
*出口参数:void
**************************************************/
void TIM2_DeInit(void)
{
  T2CON = 0X00;
#if !defined (SC92F730x) && !defined (SC92F827X) && !defined (SC92F837X) && !defined (SC92F725X) && !defined (SC92F735X)
  T2MOD = 0X00;
#endif
  TMCON &= 0XFB;
  TH2 = 0X00;
  TL2 = 0X00;
  RCAP2H = 0X00;
  RCAP2L = 0X00;
  ET2 = 0;
  IPT2 = 0;
}

/**************************************************
*函数名称:void TIM2_TimeBaseInit(TIM2_PresSel_TypeDef TIM2_PrescalerSelection,TIM2_CountMode_TypeDef TIM2_CountMode, TIM2_CountDirection_TypeDef TIM2_CountDirection)
*函数功能:TIMER2基本设置配置函数
*入口参数:
TIM2_PresSel_TypeDef:TIM2_PrescalerSelection:预分频选择
TIM2_CountMode_TypeDef:TIM2_CountMode:计数/定时模式选择
TIM2_CountDirection_TypeDef:TIM2_CountDirection:计数方向选择
*出口参数:void
**************************************************/
void TIM2_TimeBaseInit(TIM2_PresSel_TypeDef
                       TIM2_PrescalerSelection,
                       TIM2_CountMode_TypeDef TIM2_CountMode,
                       TIM2_CountDirection_TypeDef TIM2_CountDirection)
{
  if(TIM2_PrescalerSelection == TIM2_PRESSEL_FSYS_D12)
  {
    TMCON &= 0XFB;
  }
  else if(TIM2_PrescalerSelection == TIM2_PRESSEL_FSYS_D1)
  {
    TMCON |= 0X04;
  }



#if !defined (SC92F730x) && !defined (SC92F827X) && !defined (SC92F837X) && !defined (SC92F725X) && !defined (SC92F735X)\
		&& !defined (SC92F825X) && !defined (SC92F835X)

  if(TIM2_CountDirection == TIM2_COUNTDIRECTION_UP)
  {
    T2MOD &= 0XFE;
  }
  else if(TIM2_CountDirection ==
          TIM2_COUNTDIRECTION_DOWN_UP)
  {
    T2MOD |= 0X01;
  }

  if(TIM2_CountMode == TIM2_MODE_TIMER)
  {
    T2CON &= 0XFD;
  }
  else if(TIM2_CountMode == TIM2_MODE_COUNTER)
  {
    T2CON |= 0X02;
  }
	

#else
  TIM2_CountMode = 1;
  TIM2_CountDirection = 0;
#endif

}


/**************************************************
*函数名称:void TIM2_WorkMode1Config(uint16_t TIM2_SetCounter)
*函数功能:TIMER2工作模式1配置函数
*入口参数:
uint16_t:TIM2_SetCounter:配置计数初值
*出口参数:void
**************************************************/
void TIM2_WorkMode1Config(uint16_t
                          TIM2_SetCounter)
{
  RCAP2L = TIM2_SetCounter % 256;
  RCAP2H = TIM2_SetCounter / 256;
  TL2 = RCAP2L;
  TH2 = RCAP2H;
}

/**************************************************
*函数名称:void TIM2_WorkModeConfig(TIM2_WorkMode_TypeDef TIM2_WorkMode, uint16_t TIM2_SetCounter)
*函数功能:TIMER2工作模式配置函数
*入口参数:
TIM2_WorkMode_TypeDef:TIM2_WorkMode:TIMER2工作模式选择
uint16_t:TIM2_SetCounter:TIMER2计数初值配置
*出口参数:void
**************************************************/
void TIM2_WorkModeConfig(TIM2_WorkMode_TypeDef
                         TIM2_WorkMode, uint16_t TIM2_SetCounter)
{
  switch(TIM2_WorkMode)
  {
    case TIM2_WORK_MODE1:
      TIM2_WorkMode1Config(TIM2_SetCounter);
      break;
#if !defined (SC92F730x) && !defined (SC92F827X) && !defined (SC92F837X) && !defined (SC92F725X) && !defined (SC92F735X) && !defined (SC92F725X)
    case TIM2_WORK_MODE0:
      TIM2_WorkMode0Config(TIM2_SetCounter);
      break;

    case TIM2_WORK_MODE3:
      TIM2_WorkMode3Config(TIM2_SetCounter);
      break;
#endif
    default:
      break;
  }
}

#if !defined (SC92F730x) && !defined (SC92F827X) && !defined (SC92F837X) && !defined (SC92F725X) && !defined (SC92F735X)
/**************************************************
*函数名称:void TIM2_WorkMode0Config(uint16_t TIM2_SetCounter)
*函数功能:TIMER2工作模式0配置函数
*入口参数:
uint16_t:TIM2_SetCounter:配置计数初值
*出口参数:void
**************************************************/
void TIM2_WorkMode0Config(uint16_t
                          TIM2_SetCounter)
{
  T2CON |= 0x09;
  TL2 = TIM2_SetCounter % 256;
  TH2 = TIM2_SetCounter / 256;
}

/**************************************************
*函数名称:void TIM2_WorkMode3Config(uint16_t TIM2_SetCounter)
*函数功能:TIMER2工作模式3配置函数
*入口参数:
uint16_t:TIM2_SetCounter:配置计数初值
*出口参数:void
**************************************************/
void TIM2_WorkMode3Config(uint16_t
                          TIM2_SetCounter)
{
  RCAP2L = TIM2_SetCounter % 256;
  RCAP2H = TIM2_SetCounter / 256;
  T2MOD |= 0X02;
}

/*****************************************************
*函数名称:void TIM2_SetEXEN2(FunctionalState NewState)
*函数功能:TIMER2_EXEN2配置函数
*入口参数:
FunctionalState:NewState:EXEN2使能选择
*出口参数:void
*****************************************************/
void TIM2_SetEXEN2(FunctionalState NewState)
{
  if(NewState == DISABLE)
  {
    EXEN2 = 0;
  }
  else
  {
    EXEN2 = 1;
  }
}
#endif

/*****************************************************
*函数名称:void TIM2_Cmd(FunctionalState NewState)
*函数功能:TIMER2功能开关函数
*入口参数:
FunctionalState:NewState:功能启动/关闭选择
*出口参数:void
*****************************************************/
void TIM2_Cmd(FunctionalState NewState)
{
  if(NewState == DISABLE)
  {
    TR2 = 0;
  }
  else
  {
    TR2 = 1;
  }
}

/*****************************************************
*函数名称:void TIM2_ITConfig(FunctionalState NewState, PriorityStatus Priority)
*函数功能:TIMER2中断初始化
*入口参数:
FunctionalState:NewState:中断使能/关闭选择
PriorityStatus:Priority:中断优先级选择
*出口参数:void
*****************************************************/
void TIM2_ITConfig(FunctionalState NewState,
                   PriorityStatus Priority)
{
  if(NewState == DISABLE)
  {
    ET2 = 0;
  }
  else
  {
    ET2 = 1;
  }

  /************************************************************/
  if(Priority == LOW)
  {
    IPT2 = 0;
  }
  else
  {
    IPT2 = 1;
  }
}

/*****************************************************
*函数名称:FlagStatus TIM2_GetFlagStatus(void)
*函数功能:获得TIMER2中断标志状态
*入口参数:void
*出口参数:
FlagStatus:TIMER2中断标志状态
*****************************************************/
FlagStatus TIM2_GetFlagStatus(TIM2_Flag_TypeDef
                              TIM2_Flag)
{
  FlagStatus status = RESET;

  if((TIM2_Flag & T2CON) != (uint8_t)RESET)
  {
    status = SET;
  }
  else
  {
    status = RESET;
  }

  return status;
}

/*****************************************************
*函数名称:void TIM2_ClearFlag(void)
*函数功能:清除TIMER2中断标志状态
*入口参数:void
*出口参数:void
*****************************************************/
void TIM2_ClearFlag(TIM2_Flag_TypeDef TIM2_Flag)
{
  T2CON &= (~TIM2_Flag);
}

#else

/**************************************************
*函数名称:void TIM2_DeInit(void)
*函数功能:TIMER2相关寄存器复位至初始值
*入口参数:void
*出口参数:void
**************************************************/
void TIM2_DeInit()
{
  TXINX = 0x02; //TIMER2选择
  TXCON = 0X00;
  TXMOD = 0X00;
  RCAPXH = 0X00;
  RCAPXL = 0X00;
  THX = 0X00;
  TLX = 0X00;
  IE1 &= 0X3F;
  IP1 &= 0X3F;
  ET2 = 0;
  IPT2 = 0;
}

/**************************************************
*函数名称:void TIM2_PrescalerSelection(TIM2_PresSel_TypeDef TIM2_PrescalerSelection)
*函数功能:TIMER2 预分频选择
*入口参数:
TIM2_PresSel_TypeDef:TIM2_PrescalerSelection:预分频选择
*出口参数:void
**************************************************/
void TIM2_PrescalerSelection(TIM2_PresSel_TypeDef TIM2_PrescalerSelection)
{
  TXINX = 0x02;

  if (TIM2_PrescalerSelection == TIM2_PRESSEL_FSYS_D12)
  {
    TXMOD &= 0X7F;
  }
  else if (TIM2_PrescalerSelection == TIM2_PRESSEL_FSYS_D1)
  {
    TXMOD |= 0X80;
  }
}

/**************************************************
*函数名称:void TIM2_TimeBaseInit(TIM2_CountMode_TypeDef TIM2_CountMode, TIM2_CountDirection_TypeDef TIM2_CountDirection)
*函数功能:TIM2基本设置配置函数
*入口参数:
TIM2_CountMode_TypeDef:TIM2_CountMode:计数/定时模式选择
TIM2_CountDirection_TypeDef:TIM2_CountDirection:计数方向选择
*出口参数:void
**************************************************/
void TIM2_TimeBaseInit(TIM2_PresSel_TypeDef TIM2_PrescalerSelection,
                       TIM2_CountMode_TypeDef TIM2_CountMode,
                       TIM2_CountDirection_TypeDef TIM2_CountDirection)
{
  TXINX = 0x02;
	
	TXMOD &= 0X7F;
	TXMOD = TIM2_PrescalerSelection<<7;
	
  if (TIM2_CountMode == TIM2_MODE_TIMER)
  {
    TXCON &= 0XFD;
  }
  else if (TIM2_CountMode == TIM2_MODE_COUNTER)
  {
    TXCON |= 0X02;
  }

  if (TIM2_CountDirection == TIM2_COUNTDIRECTION_UP)
  {
    TXMOD &= 0XFE;
  }
  else if (TIM2_CountDirection == TIM2_COUNTDIRECTION_DOWN_UP)
  {
    TXMOD |= 0X01;
  }
}

/**************************************************
*函数名称:void TIM2_WorkMode0Config(uint16_t TIM2_SetCounter)
*函数功能:TIMER2工作模式0配置函数
*入口参数:
uint16_t:TIM2_SetCounter:配置计数初值
*出口参数:void
**************************************************/
void TIM2_WorkMode0Config(uint16_t TIM2_SetCounter)
{
  TXINX = 0x02;
  CP = 1;
  TLX = TIM2_SetCounter % 256;
  THX = TIM2_SetCounter / 256;
}

/**************************************************
*函数名称:void TIM2_WorkMode1Config(uint16_t TIM2_SetCounter)
*函数功能:TIMER2工作模式1配置函数
*入口参数:
uint16_t:TIM2_SetCounter:配置计数初值
*出口参数:void
**************************************************/
void TIM2_WorkMode1Config(uint16_t TIM2_SetCounter)
{
  TXINX = 0x02;
  RCAPXL = TIM2_SetCounter % 256;
  RCAPXH = TIM2_SetCounter / 256;

  TLX = RCAPXL;
  THX = RCAPXH;
}

/**************************************************
*函数名称:void TIM2_WorkMode3Config(uint16_t TIM2_SetCounter)
*函数功能:TIMER2工作模式3配置函数
*入口参数:
uint16_t:TIM2_SetCounter:配置计数初值
*出口参数:void
**************************************************/
void TIM2_WorkMode3Config(uint16_t TIM2_SetCounter)
{
  TXINX = 0x02;
  RCAPXL = TIM2_SetCounter % 256;
  RCAPXH = TIM2_SetCounter / 256;
  TXMOD |= 0X02;
}
/**************************************************
*函数名称:void TIM2_WorkModeConfig(TIM2_WorkMode_TypeDef TIM2_WorkMode, uint16_t TIM2_SetCounter)
*函数功能:TIMER2工作模式配置函数
*入口参数:
TIM2_WorkMode_TypeDef:TIM2_WorkMode:TIMER2工作模式选择
uint16_t:TIM2_SetCounter:TIMER2计数初值配置
*出口参数:void
**************************************************/
void TIM2_WorkModeConfig(TIM2_WorkMode_TypeDef TIM2_WorkMode, uint16_t TIM2_SetCounter)
{
  switch (TIM2_WorkMode)
  {
  case TIM2_WORK_MODE0:
    TIM2_WorkMode0Config(TIM2_SetCounter);
    break;

  case TIM2_WORK_MODE1:
    TIM2_WorkMode1Config(TIM2_SetCounter);
    break;

  case TIM2_WORK_MODE3:
    TIM2_WorkMode3Config(TIM2_SetCounter);
    break;

  default:
    break;
  }
}
/*****************************************************
*函数名称:void TIM2_SetEXEN2(FunctionalState NewState)
*函数功能:TIMER2_EXEN2配置函数
*入口参数:
FunctionalState:NewState:EXEN2使能选择
*出口参数:void
*****************************************************/
void TIM2_SetEXEN2(FunctionalState NewState)
{
  TXINX = 0x02;

  if (NewState == DISABLE)
  {
    EXENX = 0;
  }
  else
  {
    EXENX = 1;
  }
}

/*****************************************************
*函数名称:void TIM2_Cmd(FunctionalState NewState)
*函数功能:TIMER2功能开关函数
*入口参数:
FunctionalState:NewState:功能启动/关闭选择
*出口参数:void
*****************************************************/
void TIM2_Cmd(FunctionalState NewState)
{
  TXINX = 0x02;

  if (NewState == DISABLE)
  {
    TRX = 0;
  }
  else
  {
    TRX = 1;
  }
}

/*****************************************************
*函数名称:void TIM2_ITConfig(FunctionalState NewState, PriorityStatus Priority)
*函数功能:TIMER2
*入口参数:
FunctionalState:NewState:中断使能/关闭选择
PriorityStatus:Priority:中断优先级选择
*出口参数:void
*****************************************************/
void TIM2_ITConfig(FunctionalState NewState, PriorityStatus Priority)
{
  TXINX = 0x02;

  if (NewState == DISABLE)
  {
    ET2 = 0;
  }
  else
  {
    ET2 = 1;
  }

  if (Priority == LOW)
  {
    IPT2 = 0;
  }
  else
  {
    IPT2 = 1;
  }
}

/*****************************************************
*函数名称:FlagStatus TIM2_GetFlagStatus(void)
*函数功能:获得TIMER2中断标志状态
*入口参数:
TIM2_Flag_TypeDef:TIM2_Flag:TIMER2标志选择
*出口参数:
FlagStatus:TIMER2中断标志状态
*****************************************************/
FlagStatus TIM2_GetFlagStatus(TIM2_Flag_TypeDef TIM2_Flag)
{
  FlagStatus status = RESET;
  TXINX = 0x02;

  if ((TIM2_Flag & TXCON) != (uint8_t)RESET)
  {
    status = SET;
  }
  else
  {
    status = RESET;
  }

  return status;
}

/*****************************************************
*函数名称:void TIMX_ClearFlag(void)
*函数功能:清除TIMER2中断标志状态
*入口参数:
TIM2_Flag_TypeDef:TIM2_Flag:TIMER2标志选择
*出口参数:void
*****************************************************/
void TIM2_ClearFlag(TIM2_Flag_TypeDef TIM2_Flag)
{
  TXINX = 0x02;
  TXCON &= (~TIM2_Flag);
}

#endif

/******************* (C) COPYRIGHT 2020 SinOne Microelectronics *****END OF FILE****/